package cn.suimg.bilibili.service;

import cn.suimg.bilibili.util.BiliBiliUtil;
import cn.suimg.bilibili.util.Cookie;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WithReplyConsumerImpl extends DefaultBarrageConsumerImpl {

    /**
     * 直播间ID
     */
    private Integer roomId;

    /**
     * Cookie
     */
    private Cookie cookie;

    /**
     * Logger
     */
    private static final Logger logger = LoggerFactory.getLogger(WithReplyConsumerImpl.class);

    public WithReplyConsumerImpl(int roomId,Cookie cookie){
        this.roomId = roomId;
        this.cookie = cookie;
    }

    @Override
    public void onNoticeMsg(JSONObject json) {
        super.onNoticeMsg(json);
    }

    @Override
    public void onSysMsg(JSONObject json) {
        super.onSysMsg(json);
    }

    @Override
    public void onWelcome(JSONObject json) {
        super.onWelcome(json);
    }

    @Override
    public void onDanmuMsg(JSONObject json) {
        JSONArray info = json.getJSONArray("info");
        JSONArray userInfo = info.getJSONArray(2);
        String senderName = userInfo.getString(1);
        String content = info.getString(1);
        if(content.startsWith("#")){
            String reply = BiliBiliUtil.autoReply(content.replaceAll("#",""));
            //发送弹幕接口暂时无法使用，推测是cookie权限不够导致
            BiliBiliUtil.sendDanmu(roomId,String.format("@%s %s",senderName,reply),cookie);
        }else{
            super.onDanmuMsg(json);
        }
    }

    @Override
    public void onReltimeUpdate(JSONObject json) {
        super.onReltimeUpdate(json);
    }

    @Override
    public void onSendGift(JSONObject json) {
        super.onSendGift(json);
    }

    @Override
    public void onRoomRank(JSONObject json) {
        super.onRoomRank(json);
    }
}
